package org.ns.longhttp;

import org.ns.longhttp.verify.*;

import java.lang.reflect.Constructor;

public class BeanUtil {


    public static Object newInstance(String classname) {
        try {
            Class<?> aClass = Class.forName(classname);
            Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
            if (!declaredConstructor.isAccessible()) {
                declaredConstructor.setAccessible(true);
            }
            return declaredConstructor.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Class<?> getClassByName(String classname) {
        try {
            Class<?> aClass = Class.forName(classname);
            return aClass;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void throwNullException(@NotNull Object value, String var) {
        if (value == null) {
            throw new NullPointerException(String.format("%s 不能是 null", var));
        }
    }

    public static void throwZoreException(@NotZore long value, String var) {
        if (value <= 0) {
            throw new LessOrEqualsZoreException(String.format("%s 必须大于0, 当前值:%d", var, value));
        }
    }

    public static void throwEmptyException(@NotEmpty int value, String var) {
        if (value <= 0) {
            throw new StringEmptyException(String.format("%s 不可以是空的", var));
        }
    }
}
